package cn.medemede.springbootdemo1.controller;

import cn.medemede.springbootdemo1.domain.Girl;
import cn.medemede.springbootdemo1.domain.Result;
import cn.medemede.springbootdemo1.exception.GirlException;
import cn.medemede.springbootdemo1.repository.GirlRepository;
import cn.medemede.springbootdemo1.service.GirlService;
import cn.medemede.springbootdemo1.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;

@RestController
public class GrilController {

    private final GirlRepository girlRepository;
    private final GirlService girlService;

    @Autowired
    public GrilController(GirlRepository girlRepository, GirlService girlService) {
        this.girlRepository = girlRepository;
        this.girlService = girlService;
    }

    /**
     * 查询所有Girl列表
     *
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> girlList() {
        return girlRepository.findAll();
    }


    /**
     * 添加一个Girl
     *
     * @return
     */
    @PostMapping(value = "/girls")
    public Result grilAdd(@Valid Girl girl, BindingResult bindingResult) {  //@Valid开启数据验证，BindingResult获得验证结果

        if (bindingResult.hasErrors()) {
            throw new GirlException(1, bindingResult.getFieldError().getDefaultMessage());
            //return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage());
        }

        return ResultUtil.success(girlRepository.save(girl));
    }

    /**
     * 通过id查询一个Girl
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/girlsById/{id}")
    public Girl getById(@PathVariable("id") Integer id) {
        return girlRepository.findOne(id);
    }

    /**
     * 通过age查询Girl
     *
     * @param age
     * @return
     */
    @GetMapping(value = "/girlsByAge/{age}")
    public List<Girl> getByAge(@PathVariable("age") Integer age) {
        return girlRepository.findByAge(age);
    }

    /**
     * 根据主键更新Girl
     *
     * @param id
     * @param cupSize
     * @param age
     * @return
     */
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    /**
     * 根据id删除girl
     *
     * @param id
     */
    @DeleteMapping(value = "/girls/{id}")
    public void deleteById(@PathVariable("id") Integer id) {
        girlRepository.delete(id);
    }

    @GetMapping(value = "/girls/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id) throws GirlException {
        girlService.getAge(id);
    }

    @Transactional
    public void insertTwo() {

        Girl girlA = new Girl();
        Girl girlB = new Girl();
        girlA.setAge(15);
        girlB.setAge(16);
        girlA.setCupSize("C");
        girlB.setCupSize("B");

        girlRepository.save(girlA);

        girlRepository.save(girlB);
    }

}
